package test_model_database;

import static org.junit.Assert.*;

import java.sql.Date;
import java.util.ArrayList;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.servlet.model.base.*;
import com.servlet.model.database.BaseAcces;
import com.servlet.model.database.BaseAccesImpl;
import com.servlet.model.database.Database;
import com.servlet.security.MD5;

public class BaseAccesImplTest extends TestCase {

	private boolean connect;
	private BaseAccesImpl bd;
	private Database database;

	@Before
	public void setUp() throws Exception {
		super.setUp();
		bd = new BaseAccesImpl();
		connect = bd.connect();
		String url = "jdbc:postgresql://localhost:5432/propig";
		String user = "postgres";
		String passwd = "postgres";

		database = new Database(url, user, passwd);
		database.connect();
	}

	@After
	public void tearDown() throws Exception {
		bd = null;
		connect = false;
	}

	@Test
	public void testConnect() {
		Assert.assertEquals(true, connect);
	}

	@Test
	public void testDeconnect() {
		Assert.assertEquals(true, bd.deconnect());
	}

	@Test
	public void testVerifyAccount() {
		String passwd = "PDafinaBlanc0";
		passwd = MD5.generateCode(passwd);

		Assert.assertEquals(true,
				bd.verifyAccount("Dafina.Blanc@propig.m", passwd));

		passwd = "PDafinaBlanc1";
		passwd = MD5.generateCode(passwd);
		Assert.assertEquals(false,
				bd.verifyAccount("Dafina.Blanc@propig.m", passwd));
	}

	@Test
	public void testVerifyAdmin() {

		String passwd = "root";
		// passwd = MD5.generateCode(passwd);
		Assert.assertEquals(true, bd.verifyAdmin(passwd));

		passwd = "root1";
		// passwd = MD5.generateCode(passwd);
		Assert.assertEquals(false, bd.verifyAdmin(passwd));
	}

	@Test
	public void testGetPasswd() {
		String passwd = "PDafinaBlanc0";
		passwd = MD5.generateCode(passwd);
		Assert.assertEquals(passwd, bd.getPasswd("Dafina.Blanc@propig.m"));

		passwd = "PDafinaBlanc1";
		passwd = MD5.generateCode(passwd);
		Assert.assertNotSame(passwd, bd.getPasswd("Dafina.Blanc@propig.m"));
	}

	@Test
	public void testModifyPasswd() {
		String passwd = "PDafinaBlanc0";
		passwd = MD5.generateCode(passwd);

		String passwdN = "PDafinaBlanc1";
		passwdN = MD5.generateCode(passwdN);
		Assert.assertEquals(true,
				bd.modifyPasswd("Dafina.Blanc@propig.m", passwd, passwdN));

		Assert.assertEquals(passwdN, bd.getPasswd("Dafina.Blanc@propig.m"));
	}

	@Test
	public void testAddCompte() {

		String passwd = "Daf0omas4";
		passwd = MD5.generateCode(passwd);

		String email = "Dafine.Tomas@propig.m";
		String first_name = "Dafine";
		String last_name = "Tomas";
		Date birth_day = Date.valueOf("2000-03-20");
		Assert.assertEquals(true,
				bd.addCompte(email, passwd, first_name, last_name, birth_day));

		Profile profile = bd.findProfile(email);

		Assert.assertEquals(first_name, profile.getFirst_name());
		Assert.assertEquals(last_name, profile.getLast_name());
		Assert.assertEquals(birth_day, profile.getBirth_date());
		Assert.assertEquals(passwd, bd.getPasswd(email));

		email = "Dafina.Blanc@propig.m";
		Assert.assertEquals(false,
				bd.addCompte(email, passwd, first_name, last_name, birth_day));
	}

	@Test
	public void testAddNewsStringStringDateInt() {
		String email_user1 = "Dafine.Tomas@propig.m";
		String email_user2 = "Dafina.Blanc@propig.m";
		Date date_publish = Date.valueOf("2012-02-03");
		int genre = 2;
		Assert.assertEquals(true,
				bd.addNews(email_user1, email_user2, date_publish, genre));

		String selectSql = "SELECT * FROM news where email_user_1='"
				+ email_user1 + "' AND email_user_2='" + email_user2
				+ "' AND date_publish='" + date_publish + "' AND genre="
				+ genre;
		System.out.println(selectSql);
		String result = database.selectSQL(selectSql);
		Assert.assertEquals(true, result != null);
		Assert.assertEquals(true, result != "");
	}

	@Test
	public void testAddNewsStringStringDateIntString() {
		String email_user1 = "Dafine.Tomas@propig.m";
		String email_user2 = "Jacinta.Clement@propig.m";
		Date date_publish = Date.valueOf("2012-02-03");
		String info_sup = "Daena.Morel@propig.m";
		int genre = 2;
		Assert.assertEquals(true, bd.addNews(email_user1, email_user2,
				date_publish, genre, info_sup));

		String selectSql = "SELECT * FROM news where email_user_1='"
				+ email_user1 + "' AND email_user_2='" + email_user2
				+ "' AND date_publish='" + date_publish + "' AND genre="
				+ genre + " AND info_sup='" + info_sup + "'";
		System.out.println(selectSql);
		String result = database.selectSQL(selectSql);
		Assert.assertEquals(true, result != null);
		Assert.assertEquals(true, result != "");
	}

	@Test
	public void testAddStudy() {
		String email = "Dafina.Blanc@propig.m";
		String school = "Polytech";
		Date date_begin = Date.valueOf("2008-09-01");
		Date date_end = Date.valueOf("2010-09-01");
		String major = "Industries extractives";
		int city_code = 91400;
		String country = "France";
		String city = "Orsay";

		Assert.assertEquals(true, bd.addStudy(email, school, date_begin,
				date_end, major, city_code, country, city));

		String selectSql = "SELECT * FROM study where email='" + email
				+ "' AND school='" + school + "' AND date_begin='" + date_begin
				+ "' AND date_end='" + date_end + "' AND major='" + major + "'";

		String result = database.selectSQL(selectSql);
		Assert.assertEquals(true, result != null);
		Assert.assertEquals(true, result != "");
	}

	@Test
	public void testAddExperience() {
		String email = "Dafina.Blanc@propig.m";
		String enterprise = "BNP";
		Date date_begin = Date.valueOf("2008-09-01");
		Date date_end = Date.valueOf("2010-09-01");
		String post = "Info";
		Sector activity_domain = new Sector("Industries extractives", false);
		String description = "InfoPost";
		int city_code = 91400;
		String country = "France";
		String city = "Orsay";
		Assert.assertEquals(true, bd.addExperience(email, enterprise,
				date_begin, date_end, post, activity_domain, description,
				city_code, country, city));

		String selectSql = "SELECT * FROM experience where email='" + email
				+ "' AND enterprise='" + enterprise + "' AND date_begin='"
				+ date_begin + "' AND date_end='" + date_end + "' AND post='"
				+ post + "' AND activity_domain='"
				+ activity_domain.getSectorBase() + "' AND description='"
				+ description + "'";

		String result = database.selectSQL(selectSql);
		Assert.assertEquals(true, result != null);
		Assert.assertEquals(true, result != "");
	}

	@Test
	public void testAddContact() {
		String email_user1 = "Jacinte.Thomas@propig.m";
		String email_user2 = "Dafine.Tomas@propig.m";
		Date date_invitation = Date.valueOf("2011-02-23");
		Assert.assertEquals(true,
				bd.addContact(email_user1, email_user2, date_invitation));

		String selectSql = "SELECT * FROM contact where email_user_1='"
				+ email_user1 + "' AND email_user_2='" + email_user2
				+ "' AND date_invitation='" + date_invitation + "'";

		System.out.println(selectSql);
		String result = database.selectSQL(selectSql);
		Assert.assertEquals(true, result != null);
		Assert.assertEquals(true, result != "");
	}

	@Test
	public void testRemoveNews() {
		String email_user1 = "Dafine.Tomas@propig.m";
		String email_user2 = "Jacinta.Clement@propig.m";
		Date date_publish = Date.valueOf("2012-02-03");
		int genre = 2;
		News news = new News(email_user1, email_user2, date_publish, genre);

		Assert.assertEquals(true, bd.removeNews(news));
	}

	@Test
	public void testRemoveContact() {
		String email_user1 = "Jacinte.Thomas@propig.m";
		String email_user2 = "Dafine.Tomas@propig.m";
		Date date_invitation = Date.valueOf("2011-02-23");
		boolean is_accepted = false;

		Contact contact = new Contact(email_user1, email_user2, is_accepted,
				date_invitation, null);

		Assert.assertEquals(true, bd.removeContact(contact));
		String selectSql = "SELECT * FROM contact where email_user_1='"
				+ email_user1 + "' AND email_user_2='" + email_user2
				+ "' AND date_invitation='" + date_invitation + "'";

		System.out.println(selectSql);
		String result = database.selectSQL(selectSql);
		Assert.assertEquals(true, result == "");
	}

	@Test
	public void testRemoveStudy() {
		String email = "Dafina.Blanc@propig.m";
		String school = "Polytech";
		Date date_begin = Date.valueOf("2008-09-01");
		Date date_end = Date.valueOf("2010-09-01");
		String major = "Industries extractives";
		int city_code = 91400;
		String country = "France";
		String city = "Orsay";

		Area area = new Area(city_code, country, city);
		Study study = new Study(email, school, date_begin, date_end, major,
				area);
		Assert.assertEquals(true, bd.removeStudy(study));

		String selectSql = "SELECT * FROM study where email='" + email
				+ "' AND school='" + school + "' AND date_begin='" + date_begin
				+ "' AND date_end='" + date_end + "' AND major='" + major + "'";

		String result = database.selectSQL(selectSql);
		Assert.assertEquals(true, result == "");
	}

	@Test
	public void testRemoveExperience() {
		String email = "Dafina.Blanc@propig.m";
		String enterprise = "BNP";
		Date date_begin = Date.valueOf("2008-09-01");
		Date date_end = Date.valueOf("2010-09-01");
		String post = "Info";
		Sector activity_domain = new Sector("Industries extractives", false);
		String description = "InfoPost";
		int city_code = 91400;
		String country = "France";
		String city = "Orsay";
		Area area = new Area(city_code, country, city);
		Experience experience = new Experience(email, enterprise, date_begin,
				date_end, post, activity_domain, description, area);
		Assert.assertEquals(true, bd.removeExperience(experience));

		String selectSql = "SELECT * FROM experience where email='" + email
				+ "' AND enterprise='" + enterprise + "' AND date_begin='"
				+ date_begin + "' AND date_end='" + date_end + "' AND post='"
				+ post + "' AND activity_domain='"
				+ activity_domain.getSectorBase() + "' AND description='"
				+ description + "'";

		String result = database.selectSQL(selectSql);
		Assert.assertEquals(true, result == "");
	}

	@Test
	public void testUpdateProfil() {
		String email = "Dafine.Tomas@propig.m";
		String first_name = "Dafine";
		String last_name = "Tomas";
		Date birth_date = Date.valueOf("2000-03-20");

		Profile profile = new Profile(email, first_name, last_name, birth_date);

		birth_date = Date.valueOf("2000-04-20");
		profile.setBirth_date(birth_date);

		profile.setNb_contacts_par_page(3);
		profile.setNb_news_par_page(3);
		Status status = new Status("Aucun");
		profile.setStatus(status);

		profile.setPhoto("text.txt");

		Assert.assertEquals(true, bd.updateProfil(profile));

		Profile profileR = bd.findProfile(email);

		Assert.assertEquals(first_name, profileR.getFirst_name());
		Assert.assertEquals(last_name, profileR.getLast_name());
		Assert.assertEquals(birth_date, profileR.getBirth_date());
		Assert.assertEquals(3, profileR.getNb_contacts_par_page());
		Assert.assertEquals(3, profileR.getNb_news_par_page());
		Assert.assertEquals("Aucun", profileR.getStatus().getStatus());
		Assert.assertEquals("text.txt", profileR.getPhoto());
	}

	@Test
	public void testUpdateStudy() {
		Study study = bd.findStudies("Dado.Legrand@propig.m").get(0);

		Date date_begin = Date.valueOf("2010-02-20");
		study.setDateBegin(date_begin);
		Date date_end = Date.valueOf("2012-03-30");
		study.setDateEnd(date_end);
		String major = "Industries extractives";
		study.setMajor(major);
		Assert.assertEquals(true, bd.updateStudy(study));

	}
	
	@Test
	public void testUpdateContact() {
		Contact contact = bd.findContacts("Abeline.Bernard@propig.m").get(0);

		contact.setDate_acceptation(Date.valueOf("2012-02-23"));
		Assert.assertEquals(true, bd.updateContact(contact));

		String selectSql = "SELECT * FROM contact where email_user_1='"
				+ contact.getEmail_user1() + "' AND email_user_2='"
				+ contact.getEmail_user2() + "' AND date_invitation='"
				+ contact.getDate_invitation() + "' AND date_acceptation='"
				+ contact.getDate_acceptation() + "'";

		String result = database.selectSQL(selectSql);
		Assert.assertEquals(true, result != null);
		Assert.assertEquals(true, result != "");
	}

	@Test
	public void testFindArea() {

		Area area = bd.findArea(-1329605130);
		Assert.assertEquals(94828, area.getCity_code());
		Assert.assertEquals("Pays-Bas", area.getCountry());
		Assert.assertEquals("Paris", area.getCity());
	}

	@Test
	public void testExistEmail() {
		Assert.assertEquals(true, bd.existEmail("Dafina.Blanc@propig.m"));

		Assert.assertEquals(false, bd.existEmail("Dafina.Blanc@pig.m"));
	}

	@Test
	public void testFindProfile() {
		Profile profile = bd.findProfile("Dafina.Blanc@propig.m");

		Assert.assertEquals("Blanc", profile.getLast_name());
		Assert.assertEquals("Dafina", profile.getFirst_name());
		Assert.assertEquals("1993-01-01", profile.getBirth_date().toString());
		Assert.assertEquals("Dafina_Blanc.jpg", profile.getPhoto());
	}

	@Test
	public void testFindEmailByName() {
		ArrayList<String> emails = bd.findEmailByName("Blanc");

		Assert.assertEquals(true, emails.contains("Dafina.Blanc@propig.m"));
	}

	@Test
	public void testFindEmailByEnterprise() {
		ArrayList<String> emails = bd.findEmailByEnterprise("Entreprise 6");

		Assert.assertEquals(true, emails.contains("Dado.Legrand@propig.m"));
	}

	@Test
	public void testFindExperiences() {
		ArrayList<Experience> experiences = bd
				.findExperiences("Dado.Legrand@propig.m");

		int num = 0;

		for (Experience e : experiences) {
			if (e.getEnterprise().compareTo("Entreprise 9") == 0)
				num++;
			if (e.getEnterprise().compareTo("Entreprise 2") == 0)
				num++;
			if (e.getEnterprise().compareTo("Entreprise 4") == 0)
				num++;
			if (e.getEnterprise().compareTo("Entreprise 6") == 0)
				num++;
		}
		Assert.assertEquals(4, num);
	}

	@Test
	public void testFindContacts() {
		ArrayList<Contact> contacts = bd
				.findContacts("Babylas.Gauthier@propig.m");

		int num = 0;

		for (Contact c : contacts) {
			if (c.getEmail_user1().compareTo("Babette.Lefebvre@propig.m") == 0)
				num++;
			if (c.getEmail_user2().compareTo("Jacinte.Thomas@propig.m") == 0)
				num++;
		}

		Assert.assertEquals(2, num);
	}

	@Test
	public void testFindNews() {
		ArrayList<News> news = bd.findNews("Daena.Morel@propig.m");

		int num = 0;

		for (News n : news) {
			if (n.getEmail_user1().compareTo("Jacinte.Thomas@propig.m") == 0)
				num++;
			if (n.getEmail_user2().compareTo("Jacinte.Thomas@propig.m") == 0)
				num++;
			if (n.getEmail_user1().compareTo("Abelia.Faure@propig.m") == 0)
				num++;
		}
		
		Assert.assertEquals(3, num);
	}
}
